var User = require('../Schema/UserSchema');
var Classes = require('../Schema/ClassSchema');
var Students = require('../Schema/Students');
var path = require('path')
var fs = require('fs');
const moment = require('moment')
const Admin = {}

Admin.Reg = async function (req) {
  try {
    const newUser = await User.create(req.query)
    if (newUser) {
      let firstName = path.resolve(__dirname, '../public/images/', req.query.filename)
      let lastName = path.resolve(__dirname, '../public/tx/')
      fs.renameSync(firstName, `${lastName}/${newUser._id}`);
      req.session.username = req.query.username;
      return ({ code: 1, msg: '注册成功' })
    } else {
      return ({ code: 10, msg: '注册失败' })
    }
  } catch (e) {
    console.log(e);
    return ({ code: 0, msg: '服务器错误', e })
  }
}
Admin.Login = async function (req) {
  let { username, password } = req.query;
  let login = await User.findOne({ username, password })
  if (login) { //登录成功
    req.session.username = login._id;
    return { code: 0 }
  } else {
    return { code: 1 }
  }
}
Admin.isLogin = function (req) {
  if (req.session.username) {
    return { code: 0 } //存在session
  } else {
    return { code: 1 }
  }
}
Admin.AddClassName = async function (req) {
  try {
    const { name, remake } = req.body;
    let reuslt;
    if (name && remake) {
      await Classes.create({
        name,
        remake,
        time: moment().unix()
      })
      reuslt = { code: 0 }
    } else {
      reuslt = { code: 1 }
    }
    return reuslt;
  } catch (e) {
    throw e
  }
}
Admin.AddClassStudents = async function (req) {
  try {
    const { students, class_id } = req.body;
    console.log(students, class_id)
    const temp = await Classes.findOne({ _id: class_id, students });
    if (temp) {
      return { code: 33, msg: "该学生已在课程中" };
    } else {
      await Classes.updateOne({ _id: class_id }, { $push: { students } })
      const student = await Students.findOne({ _id: students });
      return { code: 0, student }
    }
  } catch (e) {
    throw e
  }
}
Admin.ClassName = function () {
  try {
    return Classes.find();
  } catch (e) {
    throw e
  }
}
Admin.DelClass = async function (req) {
  try {
    const { _id } = req.body;
    await Classes.deleteOne({ _id });
    return { code: 0 };
  } catch (e) {
    throw e
  }
}
Admin.DelStudent = async function (req) {
  try {
    const { _id } = req.body;
    await Students.deleteOne({ _id });
    return { code: 0 };
  } catch (e) {
    throw e
  }
}
Admin.QueryStudens = async function (req) {
  try {
    const { userinfo } = req.body;
    let students = await Students.find({
      $or: [
        { name: { $regex: userinfo } },
        { number: { $regex: userinfo } }
      ]
    });
    return { students };
  } catch (e) {
    throw e
  }
}
Admin.AddStudents = async function (req) {
  try {
    const { name, number } = req.body;
    let reuslt;
    if (name && number) {
      await Students.create({
        name,
        number,
        time: moment().unix()
      })
      reuslt = { code: 0 }
    } else {
      reuslt = { code: 1 }
    }
    return reuslt;
  } catch (e) {
    throw e
  }
}
Admin.getStudent = function () {
  try {
    return Students.find();
  } catch (e) {
    throw e
  }
}
Admin.ReadStudents = async function (req) {
  try {
    const { class_id } = req.body;
    const students = await Classes.findOne({ _id: class_id });
    const r = [];
    for (const student of students.students) {
      const a = await Students.find({ _id: student })
      r.push(a)
    }
    return r;
  } catch (e) {
    throw e
  }
}


module.exports = Admin